package com.sky.controller.admin;

import com.sky.constant.RedisKeyConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @author : by smomo
 * @date : 2023/7/10 12:09
 * @description :
 */

@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;





    @PostMapping()
    @ApiOperation("新增菜品")
    public Result save(@RequestBody DishDTO dishDTO)
    {
        log.info("新增菜品：{}",dishDTO);
        dishService.save(dishDTO);
        //删除缓存
        cleanCache(RedisKeyConstant.DISH_PREFIX+dishDTO.getCategoryId());
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO)
    {
        log.info("菜品分页查询:{}", dishPageQueryDTO);
        PageResult pageResult=dishService.page(dishPageQueryDTO);
        return Result.success(pageResult);
    }


    @DeleteMapping
    @ApiOperation("批量删除菜品")
    public Result delete(@RequestParam List<Long> ids)
    {
        log.info("菜品批量删除：{}", ids);
        dishService.deleteBatch(ids);
        cleanCache("dish:*");
        return Result.success();
    }

    @GetMapping("{id}")
    @ApiOperation("根据id查询菜品信息")
    public Result<DishDTO> getById(@PathVariable Long id)
    {
        DishDTO dishDTO=dishService.getById(id);
        return Result.success(dishDTO);
    }

    @PutMapping
    @ApiOperation("更新菜品信息")
    public Result update(@RequestBody DishDTO dishDTO)
    {
        dishService.updateDishWithFlavor(dishDTO);
        cleanCache(RedisKeyConstant.DISH_PREFIX+dishDTO.getCategoryId());
        return Result.success();
    }
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<DishVO>> list(Long categoryId)
    {

        //缓存菜品
        //1.拼接redis的key:dish:category
        String dishKey= RedisKeyConstant.DISH_PREFIX+categoryId;

        //2.根据key获取菜品获取value
        List<DishVO> dishVOList = (List<DishVO>) redisTemplate.opsForValue().get(dishKey);
        if(dishVOList!=null)
        {
            //2.1如果value值不为空，则直接返回数据
            return Result.success(dishVOList);
        }

        //3.查询数据库
        Dish dish = new Dish();
        dish.setCategoryId(categoryId);
        dish.setStatus(StatusConstant.ENABLE);
        List<DishVO> dishVOS = dishService.listWithFlavor(dish);
        //4.缓存穿透处理
        if(dishVOS==null||dishVOS.isEmpty())
        {
            // 4.1获取对象后，对象为空，缓存空值
            redisTemplate.opsForValue().set(dishKey,null);
        }else {
            //4.2将数据缓存到redis
            redisTemplate.opsForValue().set(dishKey,dishVOS);

        }
        return Result.success(dishVOS);
    }


    public void cleanCache(String pattern)
    {
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }


}
